package cn.hiapi.publics.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicController;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.interfaces.user.IUserService;
import cn.hiapi.shopkeeper.consts.AppConst;
import cn.hiapi.shopkeeper.entity.activity.Activity;
import cn.hiapi.shopkeeper.entity.order.ActivityInviteRecord;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.service.activity.ActivityService;
import cn.hiapi.shopkeeper.service.order.ActivityInviteRecordService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author AdinZ
 * @date 2023/2/24 17:59
 */
@RestController
@RequestMapping("/public/test")
public class TestController extends BasicController {
    private final DispatchContext dispatchContext;

    public TestController(DispatchContext dispatchContext) {
        this.dispatchContext = dispatchContext;
    }

    @GetMapping("/add-lottery")
    public ResponseEntity addLottery(@RequestParam("mobile") String mobile,
                                     @RequestParam(value = "aid", required = false) Long aid,
                                     @RequestParam(value = "count", defaultValue = "1", required = false) int count) {
        Long uid = this.dispatchContext.getServiceOne(IUserService.class).getUid(0, mobile);
        if (uid == null) return toError("手机号不存在");

        if (aid == null) aid = AppConst.ACTIVITY_LOTTERY_HOME;

        List<ActivityInviteRecord> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            ActivityInviteRecord record = new ActivityInviteRecord();
            record.setUid(1);
            record.setInviteUid(uid);
            record.setAid(aid);
            record.setShopId(0);
            record.setType(ActivityType.LOTTERY);
            record.setRemark("测试接口增加");
            list.add(record);
        }
        this.dispatchContext.getServiceOne(ActivityInviteRecordService.class).save(list);
        return toSuccess();
    }


    @GetMapping("/activity-use-end")
    public ResponseEntity activityUseEnd(@RequestParam("aid") long aid) {
        Activity activity = this.dispatchContext.getServiceOne(ActivityService.class).get(aid);
        if (activity == null) return toError("活动不存在");
        activity.setUseEnd(System.currentTimeMillis());
        this.dispatchContext.getServiceOne(ActivityService.class).save(activity);
        return toSuccess();
    }

}
